package com.hobbit.net
{
	import com.hobbit.net.core.INetConfig;
	import com.hobbit.net.core.INetService;
	
	import org.serialization.MessagePack;

	/**
	 * 通讯类
	 * @author Elendil
	 * 
	 */
	public class NetService implements INetService
	{
		private var _netConfig:INetConfig;
		
		private var _httpService:HttpService;
		
		public function NetService()
		{
			_httpService = new HttpService();
			_httpService.dataRecieved = onRecieveData;
		}
		
		public function sendHttpRequest(controllerName:String, actionName:String, data:*=null, callBack:Function=null):void
		{
			_httpService.send(controllerName, actionName, _netConfig.requestURL, data, _netConfig.method, callBack, _netConfig.dataFormat);
		}
		
		/**
		 * 请求返回
		 * @param fun 要执行的回掉
		 * @param data 返回的数据
		 * 
		 */
		private function onRecieveData(fun:Function, data:*):void
		{
			var receivedData:Object;
			if (data)
				receivedData = MessagePack.decode(data);
			trace(JSON.stringify(receivedData));
			if (fun != null)
				fun(receivedData);
		}
		
		public function sendSocketRequest():void
		{
			
		}

		/**
		 * 通讯配置，例如method、地址
		 * @return 
		 * 
		 */
		public function get netConfig():INetConfig
		{
			return _netConfig;
		}

		[Inject]
		public function set netConfig(value:INetConfig):void
		{
			_netConfig = value;
		}

	}
}